package njci.softerware.com.myexpress2017.serviceObj.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import njci.softerware.com.myexpress2017.serviceObj.*;

public class PackageRecevieDao {
    private Context context;
    private Dao<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie, Integer> packageRecevieDao;
    private DatabaseHelper helper;

    public PackageRecevieDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            packageRecevieDao = helper.getDao(njci.softerware.com.myexpress2017.serviceObj.PackageRecevie.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param packageRecevie
     * @throws SQLException
     */
    public void add(njci.softerware.com.myexpress2017.serviceObj.PackageRecevie packageRecevie) {
        try {
            packageRecevieDao.create(packageRecevie);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public njci.softerware.com.myexpress2017.serviceObj.PackageRecevie get(int id) {
        try {
            return packageRecevieDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delteExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageRecevie packageRecevie) {
        try {
            packageRecevieDao.delete(packageRecevie);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void updateExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageRecevie packageRecevie) {
        try {
            packageRecevieDao.update(packageRecevie);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> findAllUser() {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> list = null;
        try {
            list = packageRecevieDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> getpackageRecevieByUserId(Integer user_id) {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> listUser = null;
        try {
            listUser = packageRecevieDao.queryBuilder().where().eq("user_id", user_id).query();
            if (listUser != null && listUser.size() > 0) {
                return listUser;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }

    public njci.softerware.com.myexpress2017.serviceObj.PackageRecevie getpackageRecevieByUserIdAndDate(Integer user_id, String re_date) {
        PackageRecevie p = null;
        try {
            List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> listUser = packageRecevieDao.queryBuilder().where().eq("user_id", user_id).and().like("re_date", re_date).query();
            if (listUser != null && listUser.size() > 0.) {
                p = listUser.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return p;
    }

    public void addList(List<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> list) {
        Iterator<njci.softerware.com.myexpress2017.serviceObj.PackageRecevie> it = list.iterator();
        while (it.hasNext()) {
            njci.softerware.com.myexpress2017.serviceObj.PackageRecevie p = it.next();
            PackageRecevie pold;
            if ((pold = getpackageRecevieByUserIdAndDate(p.getUser_id(), p.getRe_date())) == null && p.getCount_recevie() > 0) {
                this.add(p);
            } else if (pold != null && pold.getCount_recevie() != p.getCount_recevie()&&p.getCount_recevie()>0) {
                pold.setCount_recevie(p.getCount_recevie());
                updateExpressUser(pold);
            }
        }
    }

    public Integer getListCount(int user_Id, String dataArgs, List<String> weekList) {
        int count = 0;
        try {
            QueryBuilder builder = packageRecevieDao.queryBuilder();
            List<PackageRecevie> listUser = null;
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("re_date", weekList.get(0)).or().eq("re_date", weekList.get(1)).or().eq("re_date", weekList.get(2)).or().
                        eq("re_date", weekList.get(3)).or().eq("re_date", weekList.get(4)).or().eq("re_date", weekList.get(5)).or().eq("re_date", weekList.get(6)).query();
            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("re_date", dataArgs).query();
            }
            if (listUser != null && listUser.size() > 0) {
                Iterator<PackageRecevie> it = listUser.iterator();
                while (it.hasNext()) {
                    PackageRecevie p = it.next();
                    count = count + p.getCount_recevie();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<PackageRecevie> getListBySelection(int user_Id, String dataArgs, List<String> weekList) {
        List<PackageRecevie> listUser = null;
        try {
            QueryBuilder builder = packageRecevieDao.queryBuilder();
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("re_date", weekList.get(0)).or().eq("re_date", weekList.get(1)).or().eq("re_date", weekList.get(2)).or().
                        eq("re_date", weekList.get(3)).or().eq("re_date", weekList.get(4)).or().eq("re_date", weekList.get(5)).or().eq("re_date", weekList.get(6)).query();
            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("re_date", dataArgs).query();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }

}
